<?php

namespace app\admin\controller;

use app\BaseController;
use app\dic\DicCustomer;
use app\dic\DicSite;
use think\facade\Db;
use think\App;

/**
 * @author tony
 * @class:Taskgraphqlorder 定时任务=>GraphQL获取订单顾客信息
 * @method:customer 获取顾客信息
 * @return  
 */

class Taskgraphqlorder extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
    * 方法详情
    * @param array params
    */
    public function again_update()
    {
      $time = strtotime(date('Y-m-d 00:00:00', time() - 16*3600));
      $update['graphql_status'] = 0;
      $orderin = ['121','29','86'];
      table('order')
        ->where('created_at',">",$time)
        ->where('graphql_status',"=",1)
        ->where('site_id',"in",$orderin)
        ->update($update);
      //每日确认
        $time = date('Y-m-d 00:00:00', time() - 16*3600);
        $insert['time'] = $time;
        Db::name('customerj_today')->insert($insert);
        return success('完成');
    }
    
    /**
    * 方法详情
    * @param array params
    */
    public function customer_order()
    {
        $orderin = ['121','29','86'];
        // $orderin = ['121'];
        $order = table('order')
          ->where('createtime',">=",1709222400)
          ->where('site_id',"in",$orderin)
          ->where('graphql_status',0)
          ->order('createtime desc')
          ->find();
        if(empty($order)){
          return success('暂无数据');
        }
        //临时任务
        // $customerjourneysummary = table('customerjourneysummary')->where('order_id',$order['id'])->find();
        // if(!empty($customerjourneysummary)){
        //   $update['graphql_status'] = 1;
        //   $order = table('order')
        //     ->where('id',$order['id'])
        //     ->update($update);
        //   return success('忽略');
        // }
        $site = table('site')->where('id',$order['site_id'])->find();
        $this->customer($order['id'],$site['apisecret'],$site['site_url']);
        $update['graphql_status'] = 1;
        $order = table('order')
          ->where('id',$order['id'])
          ->update($update);
        return success('完成');
    }
    
    public function customer($orderId,$code,$site_url)
    {
        // $orderId = input('order_id', '5712795861053');
        date_default_timezone_set('Asia/Shanghai');
        $url = $site_url."/admin/api/2024-01/graphql.json";
        $query = <<<QUERY
{
    order(id: "gid://shopify/Order/{$orderId}") {
      customerJourneySummary {
        firstVisit {
          id
          landingPage
          landingPageHtml
          sourceType
          utmParameters {
            campaign
            content
            medium
            source
          }
        },
        lastVisit {
          id
          landingPage
          landingPageHtml
          sourceType
          utmParameters {
            campaign
            content
            medium
            source
          }
        }
      }
    }
  }
QUERY;
        $data = posturl_business($url, $code, $query);
        $result = json_decode($data, true);
        if(!empty($result['data']['order']['customerJourneySummary']) && isset($result['data']['order']['customerJourneySummary'])){
          $customerJourneySummary = $result['data']['order']['customerJourneySummary'];
          $arr = array();
          $arr['order_id'] = $orderId;
          if(!empty($customerJourneySummary['firstVisit'])){
            $arr['firstVisit_id'] = $customerJourneySummary['firstVisit']['id'];
            $arr['firstVisit_landingPage'] = $customerJourneySummary['firstVisit']['landingPage'];
            $arr['firstVisit_landingPageHtml'] = $customerJourneySummary['firstVisit']['landingPageHtml'];
            if(!empty($customerJourneySummary['firstVisit']['utmParameters'])){
              $arr['firstVisit_campaign'] = $customerJourneySummary['firstVisit']['utmParameters']['campaign'];
              $arr['firstVisit_content'] = $customerJourneySummary['firstVisit']['utmParameters']['content'];
              $arr['firstVisit_medium'] = $customerJourneySummary['firstVisit']['utmParameters']['medium'];
              $arr['firstVisit_source'] = $customerJourneySummary['firstVisit']['utmParameters']['source'];
            }
          }
          if(!empty($customerJourneySummary['lastVisit'])){
            $arr['lastVisit_id'] = $customerJourneySummary['lastVisit']['id'];
            $arr['lastVisit_landingPage'] = $customerJourneySummary['lastVisit']['landingPage'];
            $arr['lastVisit_landingPageHtml'] = $customerJourneySummary['lastVisit']['landingPageHtml'];
            if(!empty($customerJourneySummary['lastVisit']['utmParameters'])){
              $arr['lastVisit_campaign'] = $customerJourneySummary['lastVisit']['utmParameters']['campaign'];
              $arr['lastVisit_content'] = $customerJourneySummary['lastVisit']['utmParameters']['content'];
              $arr['lastVisit_medium'] = $customerJourneySummary['lastVisit']['utmParameters']['medium'];
              $arr['lastVisit_source'] = $customerJourneySummary['lastVisit']['utmParameters']['source'];
            }
          }
          
          table('customerjourneysummary')->where('order_id',$arr['order_id'])->delete();
          table('customerjourneysummary')->insert($arr);
        }else{
          $arr = array();
          $arr['order_id'] = $orderId;
          table('customerjourneysummary')->insert($arr);
        }
        return true;
    }

    /**
    * 偶尔开启
    * @param array params
    */
    public function customerjourneysummary()
    {
      $customerjourneysummary = table('customerjourneysummary')
        ->where('firstVisit_id', '=' ,null)
        ->find();
      if(!empty($customerjourneysummary)){
        $update = array();
        $update['graphql_status'] = 0;
        table('order')->where('id',$customerjourneysummary['order_id'])->update($update);
        table('customerjourneysummary')->where('id',$customerjourneysummary['id'])->delete();
        return success('完成');
      }else{
        return success('暂无');
      } 
    }
    
}
